<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-19 20:01:26
 * @LastEditTime: 2019-10-30 17:13:24
 * @LastEditors: Please set LastEditors
 -->
<template>
  <div class="container">
    <div class="container_card">
      <div class="container_head">
        <svg-icon icon-class="list" />  渠道列表
      </div>
      <div class="container_body">
        <div class="btn_add"><el-button type="primary" @click="add_channel">添加渠道</el-button></div>
        <template>
          <el-table
            :data="tableData"
            border
          >
            <el-table-column
              prop="name"
              label="渠道名称"
            />
            <el-table-column
              prop="alias"
              label="渠道别名"
            />
            <el-table-column
              prop="platform"
              label="支持平台"
            />
            <!-- <el-table-column
              prop="code"
              sortable
              label="渠道代码"
            />
            <el-table-column
              prop="max_verify"
              sortable
              label="最高支持版本"
            />
            <el-table-column
              prop="label"
              label="角标"
            >
              <template slot-scope="scope">
                <img :src="url + scope.row.label" width="50">
              </template>
            </el-table-column>
            <el-table-column
              prop="sdk_verify"
              label="渠道sdk版本"
            /> -->
            <!-- <el-table-column
              prop="state"
              label="状态"
              filter-placement="bottom-end"
            >
              <template slot-scope="scope">
                <el-tag
                  :type="scope.row.state === '0' ? 'primary' : 'success'"
                  disable-transitions
                >
                  <div v-if="scope.row.state == 1">
                    开启
                  </div>
                  <div v-else>
                    禁用
                  </div>
                </el-tag>
              </template>
            </el-table-column> -->
            <el-table-column
              label="操作"
              width="260"
            >
              <template slot-scope="scope">
                <!-- <el-button type="text" size="small" @click="viewChannel(scope.row)">查看</el-button> -->
                <el-button type="text" size="small" @click="href_add(scope.row.id)">编辑</el-button>
                <el-button type="text" size="small" @click="view_params(scope.row.id)">配置参数</el-button>
                <!-- <el-button type="text" size="small" @click="openParams(scope.row.id)">设置参数</el-button> -->
              </template>
            </el-table-column>
          </el-table>
        </template>
      </div>
    </div>
    <!-- <el-dialog title="设置选项" :visible.sync="dialogFormVisible" /> -->

    <!-- 查看已配置参数 -->
    <el-dialog title="配置参数" :visible.sync="dialogTable">
      <el-row :gutter="20">
        <el-col :span="12">
          <template>
            <el-table
              :data="paramsTable"
              style="width: 100%"
            >
              <el-table-column
                prop="key"
                label="key"
                width="180"
              />
              <el-table-column
                prop="abstract"
                label="简介"
                width="180"
              />
            </el-table>
          </template>
        </el-col>
        <el-col :span="8">
          <el-form ref="form" :model="form" :rules="formRule">
            <el-form-item
              label="键名"
              :label-width="formLabelWidth"
              prop="key"
            >
              <el-input v-model="form.key" autocomplete="off" placeholder="请填写键名" />
            </el-form-item>
            <el-form-item
              label="简介"
              :label-width="formLabelWidth"
              prop="abstract"
            >
              <el-input v-model="form.abstract" autocomplete="off" placeholder="请填写简介" />
            </el-form-item>
            <el-form-item
              :label-width="formLabelWidth"
            >
              <el-button @click="dialogTable = false">关闭</el-button>
              <!-- <el-button v-if="dialogState" type="primary" plain @click="addInput">新 增</el-button> -->
              <el-button type="primary" @click="submitForm">添加</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import config from '../../config/config'
export default {
  data() {
    const typeValidator = (rule, value, callback) => {
      if (!this.form.type) {
        callback(new Error('请选择一种类型'))
      }
      callback()
    }
    return {
      tableData: [],
      url: config.BASE_IMG,
      form: {},
      paramsTable: [],
      dialogState: false,
      dialogTable: false,
      formLabelWidth: '120px',
      cid: 0,
      types: [
        { id: 1, name: 'input' },
        { id: 2, name: 'textarea' },
        { id: 3, name: 'checkbox' },
        { id: 4, name: 'redio' },
        { id: 5, name: 'select' }
      ],
      formRule: {
        type: [{ required: true, validator: typeValidator }],
        abstract: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        key: [{ required: true, message: '请输入键值', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.$api.getChannel([]).then(res => {
      this.tableData = res.data
    }).catch(() => {
      this.$notify.error({
        title: '错误',
        message: '网络错误！'
      })
    })
  },
  methods: {
    add_channel() {
      this.$router.push('/channel/add')
    },
    viewChannel(row) {

    },
    href_add(id) {
      this.$router.push({
        path: '/channel/add',
        query: {
          id: id
        }
      })
    },
    view_params(id) {
      const data = {
        cid: id
      }
      this.form.cid = id
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.$api.view_params(data).then(res => {
        this.paramsTable = res.data
        // for (let i = 0; i < res.data.length; i++) {
        //   // this.paramsTable[i].jsons = JSON.parse(this.paramsTable[i].json)
        // }
        loading.close()
        this.dialogTable = true
      })
    },
    showLoading() {

    },
    selectChannel() {
      if (this.form.type > 2) {
        this.dialogState = true
      } else {
        this.dialogState = false
      }
    },
    addInput() {
      const obj = {
        name: 'value'
      }
      this.form.domains.push(obj)
    },
    removeDomain() {
      if (this.form.domains.length > 1) {
        this.form.domains.pop()
      }
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // this.form.domains = JSON.stringify(this.form.domains)
          console.log(this.form)
          this.form.json = this.form.domains
          this.$api.add_params(this.form).then(res => {
            this.$refs.form.resetFields()
            this.dialogState = false
            this.view_params(this.form.cid)
          }).catch(() => {
            this.$notify.error({
              title: '错误',
              message: '网络错误！'
            })
          })
          console.log(this.form)
        }
      })
    }
  }
}
</script>

<style scoped>
  .btn_add{
    margin-bottom: 40px;
  }
  .value{
    display: flex;
    /* justify-content: space-between */
  }
</style>
